package com.action.gulimall.order.listener;

import com.action.common.constant.MqConstant;
import com.action.gulimall.order.entity.OrderEntity;
import com.action.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author sl shilun217@qq.com
 * @Deacription 订单超时消息监听,关闭订单
 * @date 2021/07/04/13:26
 **/
@Slf4j
@RabbitListener(queues = {MqConstant.ORDER_RELEASE_QUEUE_NAME})
@Component
public class OrderConsumeListener {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void releaseOrder(Message message, OrderEntity orderEntity,Channel channel) throws IOException {
        log.info("收到过期的订单信息，准备关闭订单" + orderEntity.getOrderSn());
        try {
            orderService.closeOrder(orderEntity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
